package com.lframework.xingyun.promo.impl.member;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.promo.entity.MarketingMember;
import com.lframework.xingyun.promo.mappers.MarketingMemberMapper;
import com.lframework.xingyun.promo.service.member.MarketingMemberCouponService;
import com.lframework.xingyun.promo.service.member.MarketingMemberLevelService;
import com.lframework.xingyun.promo.service.member.MarketingMemberService;
import com.lframework.xingyun.promo.vo.member.CreateMarketingMemberVo;
import com.lframework.xingyun.promo.vo.member.QueryMarketingMemberVo;
import com.lframework.xingyun.promo.vo.member.UpdateMarketingMemberVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class MarketingMemberServiceImpl extends BaseMpServiceImpl<MarketingMemberMapper, MarketingMember> implements MarketingMemberService {

    @Override
    public PageResult<MarketingMember> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingMemberVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingMember> page = getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingMember> queryList(QueryMarketingMemberVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingMember.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingMember findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增会员营销活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMarketingMemberVo vo) {

        MarketingMember data = new MarketingMember();
        data.setId(IdUtil.getId());
        data.setName(vo.getName());
        data.setBeginDate(vo.getBeginDate());
        data.setEndDate(vo.getEndDate());
        data.setPromotionsType(vo.getPromotionsType());
        data.setScopeType(vo.getScopeType());
        data.setAttendType(vo.getAttendType());
        data.setClaimMode(vo.getClaimMode());
        data.setAllMemberLevel(vo.getAllMemberLevel());
        data.setConsumeScoreFlag(vo.getConsumeScoreFlag());
        data.setConsumeScoreRate(vo.getConsumeScoreRate());
        data.setScoreFlag(vo.getScoreFlag());
        data.setScoreValue(vo.getScoreValue());
        data.setCouponFlag(vo.getCouponFlag());
        data.setDiscountFlag(vo.getDiscountFlag());
        data.setDiscountValue(vo.getDiscountValue());

        getBaseMapper().insert(data);


        getThis(MarketingMemberLevelService.class).batchAdd(data.getId(), Optional.ofNullable(vo.getMemberLevelIds()).orElseGet(ArrayList::new));

        getThis(MarketingMemberCouponService.class).batchAdd(data.getId(), Optional.ofNullable(vo.getCouponIds()).orElseGet(ArrayList::new));

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改会员营销活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMarketingMemberVo vo) {

        MarketingMember data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("会员营销活动表不存在！");
        }

        LambdaUpdateWrapper<MarketingMember> updateWrapper = Wrappers.lambdaUpdate(MarketingMember.class)
                .set(MarketingMember::getName, vo.getName())
                .set(MarketingMember::getBeginDate, vo.getBeginDate())
                .set(MarketingMember::getEndDate, vo.getEndDate())
                .set(MarketingMember::getPromotionsType, vo.getPromotionsType())
                .set(MarketingMember::getScopeType, vo.getScopeType())
                .set(MarketingMember::getAttendType, vo.getAttendType())
                .set(MarketingMember::getClaimMode, vo.getClaimMode())
                .set(MarketingMember::getAllMemberLevel, vo.getAllMemberLevel())
                .set(MarketingMember::getConsumeScoreFlag, vo.getConsumeScoreFlag())
                .set(MarketingMember::getConsumeScoreRate, vo.getConsumeScoreRate())
                .set(MarketingMember::getScoreFlag, vo.getScoreFlag())
                .set(MarketingMember::getScoreValue, vo.getScoreValue())
                .set(MarketingMember::getCouponFlag, vo.getCouponFlag())
                .set(MarketingMember::getDiscountFlag, vo.getDiscountFlag())
                .set(MarketingMember::getDiscountValue, vo.getDiscountValue())
                .eq(MarketingMember::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        getThis(MarketingMemberLevelService.class).batchUpdate(data.getId(), Optional.ofNullable(vo.getMemberLevelIds()).orElseGet(ArrayList::new));

        getThis(MarketingMemberCouponService.class).batchUpdate(data.getId(), Optional.ofNullable(vo.getCouponIds()).orElseGet(ArrayList::new));

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MarketingMember.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

